package com.example.admin.goodnews.activity;

import android.content.Intent;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.admin.goodnews.R;

import dmax.dialog.SpotsDialog;

import static com.superplayer.library.utils.SuperPlayerUtils.getScreenWidth;

/**
 * 显示图片页面
 * Created by admin on 2016/10/15.
 */

public class PhotoActivity extends AppCompatActivity {
    private ImageView iv_back_photo;// 标题栏返回按钮
    private TextView tv_content_photo;// 标题
    private ImageView iv_content_photo;// 图片展示

    private final Rect rect = new Rect();// 裁剪区域
    private BitmapRegionDecoder bitmapRegionDecoder;
    private ImageView imageView;//
    private DisplayMetrics displayMetrics;
    private int screenHeight;
    private int screenWidth;
    private SpotsDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screenHeight = displayMetrics.heightPixels;
        screenWidth = displayMetrics.widthPixels;
        setContentView(R.layout.activity_photo);
        initView();
        initData();
        initEvent();
        dialog = new SpotsDialog(PhotoActivity.this);
        dialog.show();
        // showLongpic();
    }

    private void initView() {
        iv_back_photo = (ImageView) findViewById(R.id.iv_back_photo);// 标题栏返回按钮
        tv_content_photo = (TextView) findViewById(R.id.tv_content_photo);// 标题
        iv_content_photo = (ImageView) findViewById(R.id.iv_content_photo);// 图片展示
    }

    private void initData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String pic_url = intent.getStringExtra("pic_url");
        Log.e("pic_url", pic_url);
        tv_content_photo.setText(Html.fromHtml(title).toString());
        if (pic_url.contains(".jpg")) {
            Glide.with(PhotoActivity.this).load(pic_url).diskCacheStrategy(DiskCacheStrategy.SOURCE).listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    dialog.dismiss();
                    return false;
                }
            }).animate(android.R.anim.slide_in_left).crossFade().into(iv_content_photo);
        } else if (pic_url.contains(".gif")) {
            Glide.with(PhotoActivity.this).load(pic_url).diskCacheStrategy(DiskCacheStrategy.SOURCE).listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    dialog.dismiss();
                    return false;
                }
            }).into(iv_content_photo);
        }
    }

    private void initEvent() {
        // 标题栏返回按钮
        iv_back_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void showLongpic() {
        int screenWidth = getScreenWidth(this);
        ViewGroup.LayoutParams lp = iv_content_photo.getLayoutParams();
        lp.width = screenWidth;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        iv_content_photo.setLayoutParams(lp);

        iv_content_photo.setMaxWidth(screenWidth);
        iv_content_photo.setMaxHeight(screenWidth * 5);
    }

}
